﻿using HR.Hubs;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.AspNetCore.SignalR;

namespace HR.Filters
{
    public class SignalRFilterAttribute : IAsyncActionFilter
    {
        private readonly IHubContext<InfoHub> hubContext;
        private readonly string title;

        public SignalRFilterAttribute(IHubContext<InfoHub> hubContext,string title)
        {
            this.hubContext = hubContext;
            this.title = title;
        }

        public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
        {
            //动作执行之后
            await next.Invoke();
            //如果过滤器类型等于本类型则提示：
            SignalRFilterAttribute? srf = (SignalRFilterAttribute)context.Filters.FirstOrDefault(ef => ef.GetType() == typeof(SignalRFilterAttribute));
            await hubContext.Clients.All.SendAsync("CheckInfo", srf.title, "变更审核信息待处理！");
        }
    }
}
